﻿using System.Collections.ObjectModel;

using TimeSpan.Services;

namespace TimeSpan.Views.Windows;

public partial class TaskRecordWindowViewModel(IMessageService message, WorkflowService workflow, IConfig<TaskRecordsConfig> config) : ObservableObject {

    [ObservableProperty]
    private TaskRecordsConfig allData = config.Config;

    [ObservableProperty]
    private string searchText = string.Empty;

    [ObservableProperty]
    private ObservableCollection<TaskRecord> displayData = [.. config.Config.TaskRecords];

    [RelayCommand]
    private void Search() {
        DisplayData = [.. AllData.TaskRecords.Where(x => x.Title.Contains(SearchText, StringComparison.CurrentCultureIgnoreCase)).OrderByDescending(x => x.Creation)];
    }

    [RelayCommand]
    private void Clear() { 
        SearchText = string.Empty;
        Search();
    }

    [RelayCommand]
    private async Task Delete(TaskRecord data) {
        if (!await message.ConfirmAsync("确认删除")) {  return; }
        workflow.DeleteTaskRecord.OnNext(data);
        Search();
    }

}